# 闭包 = 函数 + 环境变量
def curve_pre():
    beanOne = 25
    def curve(x):
        return beanOne*x*x
    return curve
# curve()闭包内的函数不能被直接调用
f = curve_pre()
print(f.__closure__[0].cell_contents)#获取闭包的环境变量
print(f(2))
print('---------------------------------')
#非闭包
origin = 0
def go(step):
    global origin
    new = origin + step
    origin = new
    return new
print(go(2))
print(go(3))
print(go(5))
# 闭包解决同样的问题
ori = 0
def factory(pos):
    def goto(step):
        nonlocal pos #强制声明非本地变量
        n = pos + step
        pos = n
        return n
    return goto

tourist = factory(ori)
print(tourist(2))
print(tourist(3))
print(tourist(5))